package cn.edu.xhu.library_management.common.entity;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: MMJ
 * @Date: 2022-1-12
 * @Description: 统一返回结果类
 */
@ApiModel("通用返回结果类")
@Data
public class R {
    // 是否成功
    @ApiModelProperty(value = "是否成功")
    private Boolean success;
    // 返回状态码
    @ApiModelProperty("返回状态码")
    private Integer code;
    // 返回消息
    @ApiModelProperty("返回消息")
    private String message;
    // 返回数据
    @ApiModelProperty("返回数据")
    private Map<String,Object> data = new HashMap<String, Object>();

    // 构造方法私有化
    private R() {
    }

    // 获取成功静态方法
    public static R ok() {
        R r =new R();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS.getCode());
        r.setMessage("成功");
        return r;
    }



    // 获取失败静态方法
    public static R error() {
        R r =new R();
        r.setSuccess(false);
        r.setCode(ResultCode.ERROR.getCode());
        r.setMessage("失败");
        return r;
    }
    // 操作成功
    public static R ok(String msg) {
        R r = new R();
        r.setSuccess(true);
        r.setCode(ResultCode.OPERATION_SUCCESS.getCode());
        r.setMessage(msg);
        return r;
    }
    // 操作失败
    public static R error(String msg) {
        R r = new R();
        r.setSuccess(false);
        r.setCode(ResultCode.OPERATION_ERROR.getCode());
        r.setMessage(msg);
        return r;
    }
    // 链式编程
    public R success(Boolean success){
        this.setSuccess(success);
        return this;
    }
    public R message(String message){
        this.setMessage(message);
        return this;
    }

    public R code(Integer code){
        this.setCode(code);
        return this;
    }
    public R data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map){
        this.setData(map);
        return this;
    }

}
